package com.gdit.zax.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.dao.VideoRecordDao;
import com.gdit.zax.entitys.DownRecordALL;
import com.gdit.zax.entitys.VideoALL;
import com.gdit.zax.entitys.VideoRecord;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.SqlUtil;

public class VideoRecordDaoImpl implements VideoRecordDao{
	private PreparedStatement ps;
	private ResultSet rs;

	private Connection conn;
	private HttpServletRequest request;
	private HttpServletResponse response;

	public VideoRecordDaoImpl(Connection conn, HttpServletRequest request,
			HttpServletResponse response) {
		this.conn = conn;
		this.request = request;
		this.response = response;
	}
	
	public boolean adddownrecord(VideoRecord vr) {
		String sql = "insert t_downrecord set UserId=?,VideoId=?,SectionId=?,CourseId=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, vr.getUserId());
			ps.setInt(2, vr.getVideoId());
			ps.setInt(3, vr.getSectionid());
			ps.setInt(4, vr.getCourseId());
			int result = ps.executeUpdate();
			if (result != 0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean addseerecord(VideoRecord vr) {
		String sql = "insert t_seerecord set UserId=?,VideoId=?,SectionId=?,CourseId=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, vr.getUserId());
			ps.setInt(2, vr.getVideoId());
			ps.setInt(3, vr.getSectionid());
			ps.setInt(4, vr.getCourseId());
			int result = ps.executeUpdate();
			if (result != 0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public int getDownSum(int vid) {
		String sql = "select count(Id) from t_downrecord";
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public int getSeeSum(int vid) {
		String sql = "select count(Id) from t_seerecord";
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public ArrayList<DownRecordALL> listDownRecord(int page, int onepagecount) {
		// TODO Auto-generated method stub
		return null;
	}

	public ArrayList<DownRecordALL> listMyDwonRecord(int id, int page,int onepagecount) {
		String sql = SqlUtil.getSql("getMyDownVidoList.sql");
		ArrayList<DownRecordALL> list = new ArrayList<DownRecordALL>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			ps.setInt(2, (page-1)*onepagecount);
			ps.setInt(3, onepagecount);
			
			rs = ps.executeQuery();
			while (rs.next()) {
				DownRecordALL dra = DBUtil.getJoinObject(rs, DownRecordALL.class);
				list.add(dra);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public ArrayList<VideoALL> listMySeeRecord(int id, int page,
			int onepagecount) {
		// TODO Auto-generated method stub
		return null;
	}

	public ArrayList<VideoALL> listSeeRecord(int page, int onepagecount) {
		// TODO Auto-generated method stub
		return null;
	}


}
